/* MOS                                                        */
/* Giordano Mion, Luca David Opromolla and Alessandro Sforza  */
/* University of Sussex, Banco de Portugal & NC State, and Bologna       */


program drop _all
clear all
set more off
***local path = "/Users/lucadavidopromolla/projects/MOS/stata"
***local path = "C:\Users\eeu173\Documents\Luca\Projects\MOS\stata"
local path = "/Users/lucadavidopromolla/Projects/MOS_rev"
set mem 5g



// Group destinations
use "`path'/computation/product_destination",clear
gen de_spain = (pais==11 & v_est2>0)
gen de_ooecd = ((pais==400 | pais==800 | pais==404 | pais==36 | pais==10002 | pais==61 | pais==64 | pais==24 | pais==732 | pais==728 | pais==412 | pais==28 | pais==804 | pais==10002 | pais==52 | pais==60) & v_est2>0) // USA 400 / Australia 800 / Canada 404 / Switzerland 36 / Czech Republic 10002 / Hungary 64 / Iceland 24 / Japan 732 / Korea 728 / Mexico 412 / Norway 28 / New Zealand 804 / Poland 60 / Slovakia 10002 / Turkey 52
gen de_china = (pais==720 & v_est2>0)
gen de_otop5 = ((pais==5 | pais==6 | pais==1 | pais==10000 ) & v_est2>0)  // Italy 5 / UK 6 / France 1 / Germany 10000
gen de_excol = ((pais==508 | pais==247 | pais==257 | pais==366 | pais==311 | pais==699 ) & v_est2>0)  // Brazil 508 / Angola 330 / Cape Verde 247 / Guinea-Bissau 257 / Mozambique 366 / São Tomé and Príncipe 311
gen de_oeu = ((pais==38 | pais==10003 | pais==8 | pais==32 | pais==9 | pais==7 | pais==3 | pais==30) & v_est2>0) // Austria 38 / Belgium-Luxembourg 10003 / Denmark 8 / Finland 32 / Greece 9 / Ireland 7 / Netherlands 3 / Sweden 30
gen de_row = (de_spain==0 & de_ooecd==0 & de_china==0 & de_otop5==0 & de_excol==0 & de_oeu==0 & pais!=. & v_est2>0)
gen de_ang = (pais==330 & v_est2>0)
gen de_cv = (pais==247 & v_est2>0)
gen de_gw = (pais==257 & v_est2>0)
gen de_st = (pais==311 & v_est2>0)
gen de_mz = (pais==366 & v_est2>0)
gen de_br = (pais==508 & v_est2>0)
gen de_tl = (pais==699 & v_est2>0)
gen double v_est_spain = v_est2 if (pais==11)
gen double v_est_ooecd = v_est2 if (pais==400 | pais==800 | pais==404 | pais==36 | pais==10002 | pais==64 | pais==24 | pais==732 | pais==728 | pais==412 | pais==28 | pais==804 | pais==60 | pais==10002 | pais==52) // USA 400 / Australia 800 / Canada 404 / Switzerland 36 / Czech Republic 10002 / Hungary 64 / Iceland 24 / Japan 732 / Korea 728 / Mexico 412 / Norway 28 / New Zealand 804 / Poland 60 / Slovakia 10002 / Turkey 52
gen double v_est_china = v_est2 if (pais==720)
gen double v_est_otop5 = v_est2 if (pais==5 | pais==6 | pais==1 | pais==10000)  // Italy 5 / UK 6 / France 1 / Germany 10000
gen double v_est_excol = v_est2 if (pais==508 | pais==247 | pais==257 | pais==366 | pais==311 | pais==699)  // Brazil 508 / Angola 330 / Cape Verde 247 / Guinea-Bissau 257 / Mozambique 366 / São Tomé and Príncipe 311 / East Timor 699
gen double v_est_oeu = v_est2 if (pais==38 | pais==10003 | pais==8 | pais==32 | pais==9 | pais==7 | pais==3 | pais==30) // Austria 38 / Belgium-Luxembourg 10003 / Denmark 8 / Finland 32 / Greece 9 / Ireland 7 / Netherlands 3 / Sweden 30
gen double v_est_row = v_est2 if (de_spain==0 & de_ooecd==0 & de_china==0 & de_otop5==0 & de_excol==0 & de_oeu==0 & pais!=.)
gen double v_est_ang = v_est2 if (pais==330)  // Brazil 508 / Angola 330 / Cape Verde 247 / Guinea-Bissau 257 / Mozambique 366 / São Tomé and Príncipe 311 / East Timor 699
gen double v_est_cv = v_est2 if (pais==247)  // Brazil 508 / Angola 330 / Cape Verde 247 / Guinea-Bissau 257 / Mozambique 366 / São Tomé and Príncipe 311 / East Timor 699
gen double v_est_gw = v_est2 if (pais==257)  // Brazil 508 / Angola 330 / Cape Verde 247 / Guinea-Bissau 257 / Mozambique 366 / São Tomé and Príncipe 311 / East Timor 699
gen double v_est_st = v_est2 if (pais==311)  // Brazil 508 / Angola 330 / Cape Verde 247 / Guinea-Bissau 257 / Mozambique 366 / São Tomé and Príncipe 311 / East Timor 699
gen double v_est_mz = v_est2 if (pais==366)  // Brazil 508 / Angola 330 / Cape Verde 247 / Guinea-Bissau 257 / Mozambique 366 / São Tomé and Príncipe 311 / East Timor 699
gen double v_est_br = v_est2 if (pais==508)  // Brazil 508 / Angola 330 / Cape Verde 247 / Guinea-Bissau 257 / Mozambique 366 / São Tomé and Príncipe 311 / East Timor 699
gen double v_est_tl = v_est2 if (pais==699)  // Brazil 508 / Angola 330 / Cape Verde 247 / Guinea-Bissau 257 / Mozambique 366 / São Tomé and Príncipe 311 / East Timor 699
gen dest = 1 if de_spain==1
replace dest = 2 if de_ooecd==1
replace dest = 3 if de_china==1
replace dest = 4 if de_otop5==1
replace dest = 5 if de_excol==1
replace dest = 6 if de_oeu==1
replace dest = 7 if de_row==1
replace dest = 8 if de_ang==1
replace dest = 9 if de_cv==1
replace dest = 10 if de_gw==1
replace dest = 11 if de_st==1
replace dest = 12 if de_mz==1
replace dest = 13 if de_br==1
replace dest = 14 if de_tl==1
save "`path'/computation/product_destination0",replace
collapse (max) de_* (sum) v_est_* v_est2,by(npc_alt ano pais)
gen exporting = (v_est2>0)
table ano if exporting==1,statistic(mean de_spain de_ooecd de_china de_otop5 de_excol)
table ano if exporting==1,statistic(mean de_oeu de_row)
collapse (max) de_* exporting (sum) v_est_*,by(npc_alt ano)
preserve
keep de_* exporting npc_alt ano 
compress
sort npc_alt ano
save "`path'/computation/destinations1",replace
restore
keep v_est_* npc_alt ano 
compress
sort npc_alt ano
save "`path'/computation/destinations2",replace



// Group products
use "`path'/computation/product_destination",clear
collapse (sum) v_est2,by(npc_alt ano nc)
gen exporting = (v_est2>0)
levelsof nc,local(nc)
foreach i of local nc {
gen pr_gr_`i'= nc==`i' & v_est2>0
gen double v_est_pr_gr_`i' = v_est2 if pr_gr_`i'==1
}
collapse (max) pr_gr_* exporting (sum) v_est_pr_gr_*,by(npc_alt ano)
preserve
keep pr_gr_* exporting npc_alt ano
compress
sort npc_alt ano
save "`path'/computation/productgr1", replace
restore
keep v_est_pr_gr_* npc_alt ano 
compress
sort npc_alt ano
save "`path'/computation/productgr2",replace


// Decompose total exports 
// (1) into a destination in (i) number of exported products per firm-year (in the destination) and (ii) avg. exports per product (in the destination) for a given firm-year
// (2) into a product in (i) number of export destinations (of the product) per firm-year and (ii) avg. exports per destination (of the product) for a given firm-year
use "`path'/computation/product_destination0",clear
collapse (sum) v_est2 v_est_* (max) dest ,by(npc_alt ano pais nc)
//Export variables
keep if v_est2>0  // keep only firm-year-product-country observations with positive exports
count if npc_alt!=. & ano!=. & nc!=. & v_est2==0
count if npc_alt!=. & ano!=. & pais!=. & v_est2==0
// Products 
egen firm_yr_prod = tag(npc_alt ano dest nc)
bys npc_alt ano dest:  egen double n_prod_mktgr = sum(firm_yr_prod)  // number of exported products per firm-year-destination
bys npc_alt ano dest:  egen double tot_exp = sum(v_est2)  
gen double avg_exp_mktgr = tot_exp/n_prod_mktgr   // avg. exports per product for a given firm-year-destination
replace n_prod_mktgr = log(n_prod_mktgr)
replace avg_exp_mktgr = log(avg_exp_mktgr)
// Destinations
egen firm_yr_dest = tag(npc_alt ano nc pais)
bys npc_alt ano nc:  egen double n_mkt_prgr = sum(firm_yr_dest)  // number of export destinations per firm-year-product_2digits
gen double avg_exp_prgr = tot_exp/n_mkt_prgr   // avg. exports per destination for a given firm-year-product_2digits
replace n_mkt_prgr = log(n_mkt_prgr)
replace avg_exp_prgr = log(avg_exp_prgr)
preserve
collapse (mean) n_prod_mktgr avg_exp_mktgr, by(npc_alt ano dest)
sort npc_alt ano dest
save "`path'/computation/product2.dta", replace
restore
collapse (mean) n_mkt_prgr avg_exp_prgr, by(npc_alt ano nc)
rename nc pr_gr
sort npc_alt ano pr_gr
save "`path'/computation/product3.dta", replace



// Group destinations + product-destination for Angola
use "`path'/computation/product_destination",clear
gen de_spain = (pais==11 & v_est2>0)
gen de_ooecd = ((pais==400 | pais==800 | pais==404 | pais==36 | pais==10002 | pais==64 | pais==24 | pais==732 | pais==728 | pais==412 | pais==28 | pais==804 | pais==60 | pais==10002 | pais==52) & v_est2>0) // USA 400 / Australia 800 / Canada 404 / Switzerland 36 / Czech Republic 10002 / Hungary 64 / Iceland 24 / Japan 732 / Korea 728 / Mexico 412 / Norway 28 / New Zealand 804 / Poland 60 / Slovakia 10002 / Turkey 52
gen de_china = (pais==720 & v_est2>0)
gen de_otop5 = ((pais==5 | pais==6 | pais==1 | pais==10000 ) & v_est2>0)  // Italy 5 / UK 6 / France 1 / Germany 10000
gen de_excol = ((pais==508 | pais==247 | pais==257 | pais==366 | pais==311 | pais==699 ) & v_est2>0)  // Brazil 508 / Angola 330 / Cape Verde 247 / Guinea-Bissau 257 / Mozambique 366 / São Tomé and Príncipe 311 / East Timor 699
gen de_oeu = ((pais==38 | pais==10003 | pais==8 | pais==32 | pais==9 | pais==7 | pais==3 | pais==30) & v_est2>0) // Austria 38 / Belgium-Luxembourg 10003 / Denmark 8 / Finland 32 / Greece 9 / Ireland 7 / Netherlands 3 / Sweden 30
gen de_row = (de_spain==0 & de_ooecd==0 & de_china==0 & de_otop5==0 & de_excol==0 & de_oeu==0 & pais!=. & v_est2>0)
gen de_ang = (pais==330 & v_est2>0)
gen de_cv = (pais==247 & v_est2>0)
gen de_gw = (pais==257 & v_est2>0)
gen de_st = (pais==311 & v_est2>0)
gen de_mz = (pais==366 & v_est2>0)
gen de_br = (pais==508 & v_est2>0)
gen de_tl = (pais==699 & v_est2>0)
gen double v_est_spain = v_est2 if (pais==11)
gen double v_est_ooecd = v_est2 if (pais==400 | pais==800 | pais==404 | pais==36 | pais==10002 | pais==64 | pais==24 | pais==732 | pais==728 | pais==412 | pais==28 | pais==804 | pais==60 | pais==10002 | pais==52) // USA 400 / Australia 800 / Canada 404 / Switzerland 36 / Czech Republic 10002 / Hungary 64 / Iceland 24 / Japan 732 / Korea 728 / Mexico 412 / Norway 28 / New Zealand 804 / Poland 60 / Slovakia 10002 / Turkey 52
gen double v_est_china = v_est2 if (pais==720)
gen double v_est_otop5 = v_est2 if (pais==5 | pais==6 | pais==1 | pais==10000)  // Italy 5 / UK 6 / France 1 / Germany 10000
gen double v_est_excol = v_est2 if (pais==508 | pais==247 | pais==257 | pais==366 | pais==311 | pais==699)  // Brazil 508 / Angola 330 / Cape Verde 247 / Guinea-Bissau 257 / Mozambique 366 / São Tomé and Príncipe 311 / East Timor 699
gen double v_est_oeu = v_est2 if (pais==38 | pais==10003 | pais==8 | pais==32 | pais==9 | pais==7 | pais==3 | pais==30) // Austria 38 / Belgium-Luxembourg 10003 / Denmark 8 / Finland 32 / Greece 9 / Ireland 7 / Netherlands 3 / Sweden 30
gen double v_est_row = v_est2 if (de_spain==0 & de_ooecd==0 & de_china==0 & de_otop5==0 & de_excol==0 & de_oeu==0 & pais!=.)
gen double v_est_ang = v_est2 if (pais==330)  // Brazil 508 / Angola 330 / Cape Verde 247 / Guinea-Bissau 257 / Mozambique 366 / São Tomé and Príncipe 311 / East Timor 699
gen double v_est_cv = v_est2 if (pais==247)  // Brazil 508 / Angola 330 / Cape Verde 247 / Guinea-Bissau 257 / Mozambique 366 / São Tomé and Príncipe 311 / East Timor 699
gen double v_est_gw = v_est2 if (pais==257)  // Brazil 508 / Angola 330 / Cape Verde 247 / Guinea-Bissau 257 / Mozambique 366 / São Tomé and Príncipe 311 / East Timor 699
gen double v_est_st = v_est2 if (pais==311)  // Brazil 508 / Angola 330 / Cape Verde 247 / Guinea-Bissau 257 / Mozambique 366 / São Tomé and Príncipe 311 / East Timor 699
gen double v_est_mz = v_est2 if (pais==366)  // Brazil 508 / Angola 330 / Cape Verde 247 / Guinea-Bissau 257 / Mozambique 366 / São Tomé and Príncipe 311 / East Timor 699
gen double v_est_br = v_est2 if (pais==508)  // Brazil 508 / Angola 330 / Cape Verde 247 / Guinea-Bissau 257 / Mozambique 366 / São Tomé and Príncipe 311 / East Timor 699
gen double v_est_tl = v_est2 if (pais==699)  // Brazil 508 / Angola 330 / Cape Verde 247 / Guinea-Bissau 257 / Mozambique 366 / São Tomé and Príncipe 311 / East Timor 699

gen dest = 1 if de_spain==1
replace dest = 2 if de_ooecd==1
replace dest = 3 if de_china==1
replace dest = 4 if de_otop5==1
replace dest = 5 if de_excol==1
replace dest = 6 if de_oeu==1
replace dest = 7 if de_row==1
replace dest = 8 if de_ang==1
replace dest = 9 if de_cv==1
replace dest = 10 if de_gw==1
replace dest = 11 if de_st==1
replace dest = 12 if de_mz==1
replace dest = 13 if de_br==1
replace dest = 14 if de_tl==1

levelsof nc,local(pc)
foreach h of local pc {
gen de_ang`h' = (pais==330 & v_est2>0) & (nc==`h' & v_est2>0)
gen double v_est_ang`h' = v_est2 if (pais==330 & nc==`h')
replace dest = `h' if de_ang`h'==1
}
sort npc_alt ano

save "`path'/computation/product_destination0_ap",replace
collapse (max) de_* empresa (sum) v_est_* v_est2,by(npc_alt ano pais)
gen exporting = (v_est2>0)
collapse (max) de_* exporting empresa (sum) v_est_*,by(npc_alt ano)
preserve
keep de_* exporting npc_alt ano empresa
compress
sort npc_alt ano
save "`path'/computation/destinations1_ap",replace
restore
keep v_est_* npc_alt ano empresa
compress
sort npc_alt ano
save "`path'/computation/destinations2_ap",replace
